local L = AOSS_LOCAL_STRINGS

-- Set up slash command to show the samples browser
SLASH_ADDONSTUDIOSAMPLES1 = L.SLASH1
SLASH_ADDONSTUDIOSAMPLES2 = L.SLASH2
SLASH_ADDONSTUDIOSAMPLES3 = L.SLASH3
SLASH_ADDONSTUDIOSAMPLES4 = L.SLASH4
SlashCmdList["ADDONSTUDIOSAMPLES"] = function()
	local frame = AOSSFrame
	if frame:IsShown() then
		frame:Hide()
	else
		frame:Show()
	end
end

function AOSS_OnLoad(frame)
	-- Make frame draggable
	frame:RegisterForDrag("LeftButton")
	
	-- Set up values used by OptionsFrame code
	local name = frame:GetName()
	frame.categoryFrame = _G[name.."Categories"]
	frame.panelContainer = _G[name.."PanelContainer"]
	frame.categoryList = {}
	
	-- Make sure our frame appears above all others
	frame:SetFrameLevel(UIErrorsFrame:GetFrameLevel() - 1)
	
	-- Set title
	_G[name.."HeaderText"]:SetText(L.TITLE)
end

-- Adapted from OptionsFrame_AddCategory
function AOSS_AddCategory(sample)
	local frame = AOSSFrame
	local parent = sample.parent

	if parent then
		local inParent = false
		for i, category in ipairs(frame.categoryList) do
			-- Find the category we're interested in
			if not inParent then
				if category.name == parent then
					inParent = true
					if category.hasChildren then
						sample.hidden = category.collapsed
					else
						sample.hidden = true
						category.hasChildren = true
						category.collapsed = true
					end
				end
			end
			-- Insert at the end of that category
			if inParent then
				local nextCategory = frame.categoryList[i + 1]
				if not nextCategory or not nextCategory.parent then
					tinsert(frame.categoryList, i + 1, sample)
					frame.categoryFrame:update()
					return
				end
			end
		end
	end
	
	tinsert(frame.categoryList, sample)
	frame.categoryFrame:update()	
end

function AOSS_NYI(frame)
	local NYI = frame:CreateFontString(nil, "ARTWORK", "GameFontDisable")
	NYI:SetAllPoints()
	NYI:SetText(frame.name..": Not yet implemented")
end


